package com.android.questions.ui.fragment;

import android.app.Activity;
import android.view.View;
import android.widget.ImageView;

import androidx.recyclerview.widget.RecyclerView;

import com.android.base.util.ActivityClickUtil;
import com.android.base.util.DQLog;
import com.android.questions.R;
import com.android.questions.config.LoginConfig;
import com.android.questions.ui.adapter.TaskRewardAdapter;
import com.android.questions.ui.dialog.DialogAddCoinResult;
import com.android.questions.util.Command;
import com.android.questions.util.Market;
import com.dq.ad.UMManager;
import com.hx.currency.HXCurrencySDK;
import com.hx.currency.data.qa.api.QATaskCompleteResp;
import com.hx.currency.data.qa.api.QATaskConfigResp;
import com.hx.currency.data.qa.model.QATask;
import com.hx.currency.data.qa.model.QATaskType;
import com.lxj.xpopup.core.BottomPopupView;
import com.qq.e.o.data.HttpUtil;
import com.qq.e.o.utils.JsonUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * FileName: TaskLayoutView
 * Author: admin
 * Date: 2021/11/2 9:26
 * Description:
 */
public class TaskLayoutView extends BottomPopupView implements TaskRewardAdapter.OnItemClickListener, View.OnClickListener {

    private TaskRewardAdapter rewardAdapter;
    private final Activity activity;

    private DialogAddCoinResult dialogTaskReceiveResult;

    public TaskLayoutView(Activity act) {
        super(act);
        this.activity = act;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.layout_task;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        initView();
        initData();
        initLister();
    }

    public void initView() {
        ActivityClickUtil.getInstance().clear();
        UMManager.onEvent(UMManager.Event.task_click);
        RecyclerView recyclerViewReward = findViewById(R.id.recyclerViewReward);
        rewardAdapter = new TaskRewardAdapter();
        recyclerViewReward.setLayoutManager(Command.Method.getLinearLayoutManager(activity));
        recyclerViewReward.setAdapter(rewardAdapter);
    }

    public void initData() {
        getTaskData();
    }

    public void initLister() {
        rewardAdapter.setOnItemClickListener(this);
        findViewById(R.id.imgClose).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    private void getTaskData() {
        HXCurrencySDK.getInstance().getQATaskConfig(new HttpUtil.HttpUtilCallback() {
            @Override
            public void onSuccess(int i, String response) {
                DQLog.e("任务列表获取成功：" + response);
                if (activity.isFinishing()) {
                    return;
                }
                QATaskConfigResp qaTaskConfigResp = JsonUtil.parseObject(response, QATaskConfigResp.class);
                if (qaTaskConfigResp != null) {
                    if (qaTaskConfigResp.getErrorCode() == 0) {
                        updateUI(qaTaskConfigResp.getTtl());
                    } else {
                        DQLog.e("任务列表获取异常：" + qaTaskConfigResp.getErrorMessage());
                    }
                } else {
                    DQLog.e("任务列表获取异常");
                }
            }

            @Override
            public void onFailed(int i, Throwable throwable) {
                DQLog.e("任务列表获取异常：" + throwable.toString());
            }
        });
    }

    private void updateUI(List<QATaskType> qaTaskTypeList) {
        for (QATaskType qaTaskType : qaTaskTypeList) {
            if (qaTaskType.getType() == 1) {//新手任务
                List<QATask> tl = qaTaskType.getTl();
                rewardAdapter.setList(tl);

                rewardAdapter.removeAllFooterView();
                DQLog.e("任务列表获取成功：" + tl.size());

                List<QATask> qaTaskList = new ArrayList<>();
                for (int index = 0; index < tl.size(); index++) {
                    if (index == 0) {
                        continue;
                    }
                    QATask qaTask = tl.get(index);
                    if (qaTask.getTs() != 2) {
                        qaTaskList.add(qaTask);
                    }
                }

                if (qaTaskList.size() > 6) {
                    rewardAdapter.setList(qaTaskList.subList(0, 6));
                    View bottomView = activity.getLayoutInflater().inflate(R.layout.layout_task_bottom, null);
                    rewardAdapter.addFooterView(bottomView);
                } else {
                    rewardAdapter.setList(qaTaskList);
                }
            }
        }
        if (rewardAdapter != null) {
            rewardAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onItemReward(int position, QATask qaTask) {
        if (qaTask.getTct() == 4) {
            Market.start(activity);
        }
        DQLog.e("任务上报：" + qaTask.getTid());
        UMManager.onEvent(UMManager.Event.task_receive);
        reportTask(qaTask.getTid(), 1);
    }

    private void reportTask(int taskId, int taskType) {
        HXCurrencySDK.getInstance().qaTaskComplete(taskId, taskType, new HttpUtil.HttpUtilCallback() {
            @Override
            public void onSuccess(int i, String response) {
                DQLog.e("任务上报成功：" + response);
                if (activity.isFinishing()) {
                    return;
                }
                QATaskCompleteResp qaTaskCompleteResp = JsonUtil.parseObject(response, QATaskCompleteResp.class);
                if (qaTaskCompleteResp != null) {
                    if (qaTaskCompleteResp.getErrorCode() == 0) {
                        updateUI2(qaTaskCompleteResp);
                    } else {
                        DQLog.e("任务上报异常：" + qaTaskCompleteResp.getErrorMessage());
                    }
                } else {
                    DQLog.e("任务上报异常");
                }
            }

            @Override
            public void onFailed(int i, Throwable throwable) {
                DQLog.e("任务上报异常：" + throwable.getMessage());
            }
        });
    }

    private void updateUI2(QATaskCompleteResp qaTaskCompleteResp) {
        if (dialogTaskReceiveResult != null) {
            dialogTaskReceiveResult.dismiss();
            dialogTaskReceiveResult = null;
        }
        double rpa = qaTaskCompleteResp.getRpa();
        if (rpa > 0) {//红包金额
            LoginConfig.setUserCash(LoginConfig.getUserCash() + rpa);

            dialogTaskReceiveResult = new DialogAddCoinResult(activity)
                    .setType(DialogAddCoinResult.TYPE_TASK)
                    .setMoney(rpa)
                    .setClickLister(new DialogAddCoinResult.OnClickLister() {
                        @Override
                        public void onReceive() {

                        }
                    });
            dialogTaskReceiveResult.show();
        }

        getTaskData();
    }

    @Override
    public void onClick(View v) {
        if (ActivityClickUtil.getInstance().isFastClick()) {
            //防止用户快速点击
            return;
        }
//        if (v.getId() == R.id.imgTopTaskComplete) {
//            if (LoginConfig.getUserAnswerSuccessNumber() > 19 && LoginConfig.getUserAnswerSuccessNumber() % 20 == 0) {
//
//            }
//        } else if (v.getId() == R.id.imgWithdrawal) {
//
//        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (dialogTaskReceiveResult != null) {
            dialogTaskReceiveResult.dismiss();
            dialogTaskReceiveResult = null;
        }
    }
}